package com.mycompany.inventory.persistence;

import com.mycompany.common.AbstractDao;
import com.mycompany.common.jdbc.ConnectionDatabase;
import com.mycompany.inventory.pojo.BillMaterial;
import com.mycompany.inventory.pojo.Type;

import java.util.ArrayList;
import java.util.List;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by My Company.
 * User: loitran
 * Date: Feb 29, 2012
 * Time: 1:53:19 PM
 * com.mycompany.inventory.persistence.
 */
public class BillMaterialDAO extends AbstractDao<BillMaterial>{
	
	private Connection con = this.connect;
	public static final String SELECT_ALL_SQL = "select * from oce_bill_of_material";
	public static final String SELECT_SQL = "select * from oce_bill_of_material where billOfMaterialId = ?";
	public static final String SELECT_BY_PRODUCT_ID_SQL = "select * from oce_bill_of_material where product_id = ?";
	public static final String SELECT_QUANTITY_SQL = "select * from oce_bill_of_material where product_id = ? AND material_id = ?";
	public static final String INSERT_SQL = "INSERT INTO oce_bill_of_material (billOfMaterialId,product_id,material_id,number_of_material) VALUES (?,?,?,?)";
	public static final String UPDATE_SQL = "UPDATE oce_bill_of_material SET number_of_material = ? WHERE product_id = ? and material_id = ?";
	
	public BillMaterial create(BillMaterial obj){
		try{
			PreparedStatement ps = con.prepareStatement(INSERT_SQL, PreparedStatement.RETURN_GENERATED_KEYS);	
			long id = 0l;
			ps.setLong(1,id);
			ps.setString(2, obj.getProductId());
			ps.setString(3, obj.getMaterialId());
			ps.setString(4, obj.getNumber());
			ConnectionDatabase.executeUpdate(ps);	
			ResultSet rs=ps.getGeneratedKeys();
            if(rs.next()){
                Integer Id = (Integer) rs.getInt(1);               
                obj.setId(String.valueOf(Id));
            }
			ps.close();			
			
		}catch (Exception e) {
			// TODO: handle exception
			obj = null;
		}
		return obj;
	}
	
	public BillMaterial update(BillMaterial obj){
		try{
			PreparedStatement ps = con.prepareStatement(UPDATE_SQL, PreparedStatement.RETURN_GENERATED_KEYS);	
			long id = 0l;
			ps.setLong(1, Long.parseLong(obj.getNumber()));
			ps.setLong(2, Long.parseLong(obj.getProductId()));
			ps.setLong(3, Long.parseLong(obj.getMaterialId()));			
			ConnectionDatabase.executeUpdate(ps);			
		}catch (Exception e) {
			// TODO: handle exception
		}
		return obj;
	}
	
	public BillMaterial find(long id){
		List<BillMaterial> materials = new ArrayList<BillMaterial>();
		try{
			PreparedStatement ps = con.prepareStatement(SELECT_SQL, PreparedStatement.RETURN_GENERATED_KEYS);
			ps.setLong(1,id);
			materials = convert(ConnectionDatabase.executeQuery(ps));
		}catch (Exception e) {
			// TODO: handle exception
		}
		return materials.size()== 0 ? null : materials.get(0);
	}
	
	public List<BillMaterial> findAll(){
		List<BillMaterial> materials = new ArrayList<BillMaterial>();
		try{
			materials = convert(ConnectionDatabase.executeQuery(con.prepareStatement(SELECT_ALL_SQL, PreparedStatement.RETURN_GENERATED_KEYS)));
		}catch (Exception e) {
			// TODO: handle exception
			materials = null;
		}
		return materials;
	}

	public List<BillMaterial> findByProductId(long Id){
		List<BillMaterial> materials = new ArrayList<BillMaterial>();
		try{
			PreparedStatement ps = con.prepareStatement(SELECT_BY_PRODUCT_ID_SQL, PreparedStatement.RETURN_GENERATED_KEYS);
			ps.setLong(1,Id);
			materials = convert(ConnectionDatabase.executeQuery(ps));
		}catch (Exception e) {
			// TODO: handle exception
			materials = null;
		}
		return materials;
	}	

	public List<BillMaterial> findMaterial(long productId,long materialId){
		List<BillMaterial> materials = new ArrayList<BillMaterial>();
		try{
			PreparedStatement ps = con.prepareStatement(SELECT_QUANTITY_SQL, PreparedStatement.RETURN_GENERATED_KEYS);
			ps.setLong(1,productId);
			ps.setLong(2,materialId);
			materials = convert(ConnectionDatabase.executeQuery(ps));
		}catch (Exception e) {
			// TODO: handle exception
			materials = null;
		}
		return materials;
	}	
	
	public void delete(BillMaterial obj){
		try{
			
		}catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	private List<BillMaterial> convert(ResultSet rs) throws SQLException {
		List<BillMaterial> materials = new ArrayList<BillMaterial>();
		while (rs.next()) {
	        // Get the data from the row using the column index
	        String id = rs.getString(1);
	        String productId = rs.getString(2);
	        String materialId = rs.getString(3);
	        String number = rs.getString(4);
	        materials.add(new BillMaterial(id,productId,materialId,number));
	    }
		rs.close();
		return materials;
	}
}
